package application;

import java.util.ArrayList;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
import util.News;

public class Animation implements Runnable {
	Circle c4 = new Circle(5,Color.BROWN);
	Circle c2 = new Circle(8,Color.DARKBLUE);
	Shape star,mgtri;
	Polygon tri1 = new Polygon();
	Polygon tri2 = new Polygon();
	Polygon tri3 = new Polygon();
	Circle c3 = new Circle(8,Color.DARKBLUE);
	Circle c5 = new Circle(5,Color.BROWN);
	TranslateTransition translateTransitionC4;
	TranslateTransition translateTransitionC5;
	TranslateTransition translateTransitionC2;
	TranslateTransition translateTransitionC3;
	RotateTransition rotateTransition;
	ParallelTransition parallelTransition;
	MainController mainCtrl;
	TilePane tp;
	FlowPane fp;
	static boolean ispress=false;  //判断用户是否按了刷新键
	/**
	 * @param tp 装载整个动画的tilepane
	 * @param fp 装载动画图形的flowpane
	 * @author TAO
	 */
	public Animation(TilePane tp,FlowPane fp) {
		tri1.getPoints().addAll(new Double[]{
    		    19.02, 0.0,
    		    7.265, 36.18,
    		    30.777, 36.18});
    	tri2.getPoints().addAll(new Double[]{
    		    0.0, 13.82,
    		    38.04, 13.82,
    		    19.02, 27.64});
    	tri3.getPoints().addAll(new Double[]{
    			19.02, 27.64,
    		    7.265, 36.18,
    		    30.777, 36.18});
    	mgtri = Shape.union(tri1, tri2);
    	star = Shape.subtract(mgtri, tri3);
    	star.setFill(Color.rgb(255, 55, 49));
    	star.setTranslateX(200);c2.setTranslateX(200);c3.setTranslateX(200);c4.setTranslateX(200);c5.setTranslateX(200);
        translateTransitionC4=new TranslateTransition(Duration.millis(2000), c4);
        translateTransitionC4.setFromX(200);
        translateTransitionC4.setToX(20);
        translateTransitionC4.setCycleCount(2);
        translateTransitionC4.setAutoReverse(true);
        translateTransitionC5=new TranslateTransition(Duration.millis(2000), c5);
        translateTransitionC5.setFromX(200);
        translateTransitionC5.setToX(380);
        translateTransitionC5.setCycleCount(2);
        translateTransitionC5.setAutoReverse(true);
        translateTransitionC2=new TranslateTransition(Duration.millis(2000), c2);
        translateTransitionC2.setFromX(200);
        translateTransitionC2.setToX(100);
        translateTransitionC2.setCycleCount(2);
        translateTransitionC2.setAutoReverse(true);
        translateTransitionC3=new TranslateTransition(Duration.millis(2000), c3);
        translateTransitionC3.setFromX(200);
        translateTransitionC3.setToX(300);
        translateTransitionC3.setCycleCount(2);
        translateTransitionC3.setAutoReverse(true);
        rotateTransition = new RotateTransition(Duration.millis(2000), star);
        rotateTransition.setByAngle(72);//旋转度数
        rotateTransition.setCycleCount(2);
        parallelTransition=new ParallelTransition(translateTransitionC4,translateTransitionC5,translateTransitionC2,translateTransitionC3,rotateTransition);
        parallelTransition.setCycleCount(Timeline.INDEFINITE);
    	this.tp=tp;
    	this.fp=fp;
    	fp.getChildren().clear();
    	fp.getChildren().addAll(c4, c2, star, c3, c5);
	}
    public void run() {
        parallelTransition.play();
        Time.delay(5);  // 延时5秒
        if(ispress==false) {
        	mainCtrl.s.stopCrew();
        	tp.setVisible(false);
        	Platform.runLater(new Runnable() {   //等待JavaFX线程来执行
        		@Override
        		public void run() {
        			fp.getChildren().clear();
        			ArrayList<News> news = new ArrayList<>();
        			news = mainCtrl.s.getNewsByTitleKeyWord(mainCtrl.inputContent);
        			if(news.size()==0) mainCtrl.setNotFoundInfo();
        			else mainCtrl.setPagination(news);
        		}
        	});
        } 
    }
    /**
     *  手动开启动画
     */
    public void handRunAm() {
    	parallelTransition.play();
    }
    
    public void setMainCtrl(MainController mainCtrl) {
    	this.mainCtrl=mainCtrl;
    }
}
